﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using VSA.DBUtility;
using VSA.Entity;

namespace VSA.DAL
{
    public class FunctionsDAL
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Did)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Function where Did=@Did ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Did", DbType.Int32, Did);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public bool Exists(string value)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Function where FunctionName=@FunctionName ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "FunctionName", DbType.AnsiString, value);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(VSA.Entity.Functions model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Function(");
            strSql.Append("FunctionName,CreateDate)");

            strSql.Append(" values (");
            strSql.Append("@FunctionName,@CreateDate)");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "FunctionName", DbType.AnsiString, model.FunctionName);
            db.AddInParameter(dbCommand, "CreateDate", DbType.AnsiString, DateTime.Now.ToString());
            return db.ExecuteNonQuery(dbCommand) > 0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(VSA.Entity.Functions model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Function set ");
            strSql.Append("FunctionName=@FunctionName");
            strSql.Append(" where Did=@Did ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());

            db.AddInParameter(dbCommand, "FunctionName", DbType.AnsiString, model.FunctionName);
            db.AddInParameter(dbCommand, "Did", DbType.Int32, model.Did);
            return db.ExecuteNonQuery(dbCommand) > 0;

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Did)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Function ");
            strSql.Append(" where Did=@Did ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Did", DbType.Int32, Did);
            return db.ExecuteNonQuery(dbCommand) > 0;

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public VSA.Entity.Functions GetModel(int Did)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Did,FunctionName,CreateDate from t_Function ");
            strSql.Append(" where Did=@Did ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Did", DbType.Int32, Did);
            VSA.Entity.Functions model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Did,FunctionName,CreateDate ");
            strSql.Append(" FROM t_Function ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<VSA.Entity.Functions> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Did,FunctionName,CreateDate ");
            strSql.Append(" FROM t_Function ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<VSA.Entity.Functions> list = new List<VSA.Entity.Functions>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public VSA.Entity.Functions ReaderBind(IDataReader dataReader)
        {
            VSA.Entity.Functions model = new VSA.Entity.Functions();
            object ojb;
            ojb = dataReader["Did"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Did = (int)ojb;
            }
            model.FunctionName = dataReader["FunctionName"].ToString();
            ojb = dataReader["CreateDate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CreateDate = (DateTime)ojb;
            }
            return model;
        }
    }
}